package br.com.DoceMagia.view.telas;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import br.com.DoceMagia.Controller.GenericController;
import br.com.DoceMagia.modelo.dao.UsuarioDAO;

import javax.swing.ButtonGroup;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import java.awt.Component;

public class CadastroUsuario extends JFrame {

	private JPanel contentPane;
	private JTextField txtNomeUser;
	private JTextField txtLoginUser;
	private JPasswordField txtSenhaUser;
	private JPasswordField txtConfSenhaUser;
	private Integer userAdmin = 1;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CadastroUsuario frame = new CadastroUsuario();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CadastroUsuario() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setLocationRelativeTo(null);
		setTitle("Agenda Doce Magia - v1.0");
		setBounds(100, 100, 500, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNomeUser = new JLabel("Nome: ");
		lblNomeUser.setBounds(19, 50, 61, 14);
		contentPane.add(lblNomeUser);
		
		txtNomeUser = new JTextField();
		txtNomeUser.setToolTipText("Nome completo do usu\u00E1rio.");
		txtNomeUser.setColumns(10);
		txtNomeUser.setBounds(105, 47, 344, 20);
		contentPane.add(txtNomeUser);
		
		JLabel lblLoginUser = new JLabel("Login:");
		lblLoginUser.setBounds(19, 77, 61, 14);
		contentPane.add(lblLoginUser);
		
		txtLoginUser = new JTextField();
		txtLoginUser.setToolTipText("Login de acesso no sistema.");
		txtLoginUser.setColumns(10);
		txtLoginUser.setBounds(105, 74, 344, 20);
		contentPane.add(txtLoginUser);
		
		JLabel lblSenhaUser = new JLabel("Senha: ");
		lblSenhaUser.setBounds(19, 159, 47, 14);
		contentPane.add(lblSenhaUser);
		
		txtSenhaUser = new JPasswordField();
		txtSenhaUser.setToolTipText("Senha com 6 d\u00EDgitos");
		txtSenhaUser.setColumns(6);
		txtSenhaUser.setBounds(66, 156, 130, 20);
		contentPane.add(txtSenhaUser);
		
		JLabel lblConfSenhaUser = new JLabel("Confirmar senha: ");
		lblConfSenhaUser.setBounds(206, 159, 106, 14);
		contentPane.add(lblConfSenhaUser);
		
		txtConfSenhaUser = new JPasswordField();
		txtConfSenhaUser.setToolTipText("Digite a mesma senha com 6 d\u00EDgitos");
		txtConfSenhaUser.setColumns(6);
		txtConfSenhaUser.setBounds(310, 156, 123, 20);
		contentPane.add(txtConfSenhaUser);
		
		JLabel lblNivelAcesso = new JLabel("N\u00EDvel de Acesso:");
		lblNivelAcesso.setBounds(19, 102, 123, 14);
		contentPane.add(lblNivelAcesso);
		
		JCheckBox chkAdmin = new JCheckBox("Administrador");
		buttonGroup.add(chkAdmin);
		chkAdmin.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
			userAdmin = 0;
			}
		});
		chkAdmin.setToolTipText("Usu\u00E1rio que fez todas as altera\u00E7\u00F5es no sistema.");
		chkAdmin.setBounds(19, 119, 114, 23);
		contentPane.add(chkAdmin);
		
		JCheckBox chkUsuario = new JCheckBox("Usu\u00E1rio");
		buttonGroup.add(chkUsuario);
		
		chkUsuario.setToolTipText("Usu\u00E1rio com fun\u00E7\u00F5es limitadas no sistema.");
		chkUsuario.setSelected(true);
		chkUsuario.setBounds(135, 119, 86, 23);
		contentPane.add(chkUsuario);
		
		JButton btCancelar = new JButton("Cancelar");
		btCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			setVisible(false);
			}
		});
		btCancelar.setBounds(186, 216, 89, 23);
		contentPane.add(btCancelar);
		
		
		JButton btSalvar = new JButton("Salvar");
		btSalvar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				GenericController cadUserController = new GenericController();
				try {
					
				cadUserController.verificaCampoVazio(txtNomeUser, txtNomeUser.getText());
				cadUserController.verificaCampoVazio(txtLoginUser, txtLoginUser.getText());
				
				cadUserController.compareTexto(getTxtSenhaUser(), getTxtConfSenhaUser(), getTxtSenhaUser().getPassword(), getTxtConfSenhaUser().getPassword());
				UsuarioDAO usuarioDAO = new UsuarioDAO();
				usuarioDAO.InsertUsuario(getTxtLoginUser().getText(), getTxtSenhaUser().getPassword(), getTxtNomeUser().getText(), getUserAdmin().intValue());
				} catch (Exception e) {
				e.printStackTrace();
				}
			}
		});
		btSalvar.setBounds(322, 216, 89, 23);
		contentPane.add(btSalvar);
		
		JLabel lblTitulo = new JLabel("Cadastro de Usu\u00E1rios do Sistema");
		lblTitulo.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblTitulo.setBounds(87, 11, 274, 14);
		contentPane.add(lblTitulo);
		setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{contentPane, lblNomeUser, txtNomeUser, lblLoginUser, txtLoginUser, lblSenhaUser, txtSenhaUser, lblConfSenhaUser, txtConfSenhaUser, lblNivelAcesso, chkAdmin, chkUsuario, btCancelar, btSalvar, lblTitulo}));
	
	
	}

	public Integer getUserAdmin() {
		return userAdmin;
	}

	public void setUserAdmin(Integer userAdmin) {
		this.userAdmin = userAdmin;
	}

	public JTextField getTxtNomeUser() {
		return txtNomeUser;
	}

	public void setTxtNomeUser(JTextField txtNomeUser) {
		this.txtNomeUser = txtNomeUser;
	}

	public JTextField getTxtLoginUser() {
		return txtLoginUser;
	}

	public void setTxtLoginUser(JTextField txtLoginUser) {
		this.txtLoginUser = txtLoginUser;
	}

	public JPasswordField getTxtSenhaUser() {
		return txtSenhaUser;
	}

	public void setTxtSenhaUser(JPasswordField txtSenhaUser) {
		this.txtSenhaUser = txtSenhaUser;
	}

	public JPasswordField getTxtConfSenhaUser() {
		return txtConfSenhaUser;
	}

	public void setTxtConfSenhaUser(JPasswordField txtConfSenhaUser) {
		this.txtConfSenhaUser = txtConfSenhaUser;
	}
	
	
}
